package com.jn.erp.web.api.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.common.entity.TLabel;
import com.jn.erp.web.api.common.service.TLabelService;
import com.jn.erp.web.api.course.entity.TCourseOrderConfig;
import com.jn.erp.web.api.course.service.TCourseOrderConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyp
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/tLabel")
public class TLabelController extends BaseWebAuthController {
    @Autowired
    private TLabelService tLabelService;

    @Autowired
    private TCourseOrderConfigService tCourseOrderConfigService;

    /**
     教室新增新建
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody TLabel tLabel){
        //机构id
        tLabel.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tLabel.setCreateUser(String.valueOf(getUserId()) );
        tLabel.setCreateDate(LocalDateTime.now() );
        tLabel.setIsDelete("0");
        Boolean boo = tLabelService.save(tLabel);
        return AjaxResult.success(boo);
    }

    /**
     标签批量新增修改
     */
    @PostMapping("updateBatch")
    public AjaxResult updateBatch(@RequestBody List<TLabel> tLabels){
        TLabel tLabel = new TLabel();
        //当前登录人id
        tLabel.setOrgId(Long.parseLong(loginDeptAuth()));
        tLabel.setUpdateUser(String.valueOf(getUserId()) );
        tLabel.setUpdateDate(LocalDateTime.now());
        tLabel.setIsDelete("1");
        String code = tLabelService.updateBatch(tLabel,tLabels);

        return AjaxResult.success(code);
    }

    /**
     教室修改
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody TLabel tLabel){
        //机构id
        tLabel.setUpdateDate(LocalDateTime.now() );
        //当前登录人id
        tLabel.setUpdateUser(String.valueOf(getUserId()) );
        Boolean boo = tLabelService.updateById(tLabel);
        return AjaxResult.success(boo);
    }

    /**
     列表
     */
    @PostMapping("list")
    public AjaxResult list(@RequestBody TLabel tLabel){

        List<TLabel> list = tLabelService.list(new QueryWrapper<TLabel>().eq("org_id",Long.parseLong(loginDeptAuth())).eq("is_delete","0").eq("lable_type",tLabel.getLableType()));
        return AjaxResult.success(list);
    }

    /**
     教室列表
     */
    @PostMapping("classRoomList")
    public AjaxResult classRoomList(@RequestBody TLabel tLabel){
        //当前登录人id
        tLabel.setOrgId(Long.parseLong(loginDeptAuth()));
        tLabel.setUpdateUser(String.valueOf(getUserId()) );
        tLabel.setUpdateDate(LocalDateTime.now());
        List<TLabel> tLabelList = tLabelService.classRoomList(tLabel);

        return AjaxResult.success(tLabelList);
    }

    /**
     教室删除
     */
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody TLabel tLabel){
        //机构id
        tLabel.setUpdateDate(LocalDateTime.now() );
        //当前登录人id
        tLabel.setUpdateUser(String.valueOf(getUserId()) );
        tLabel.setIsDelete("1");
        Boolean boo = tLabelService.updateById(tLabel);
        return AjaxResult.success(boo);
    }
}

